#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "../include/main_header.h"
#include "../include/block.h"
#include "../include/free_list.h"
#include "../include/file_descriptor.h"
#include "../include/trie.h"

struct fdreturnList* addtoList(struct fdreturnList *list, struct file_descriptor *fd)
{
	struct fdreturnList *temp;
	temp = (struct fdreturnList*) malloc (sizeof(struct fdreturnList));
	temp -> fdescriptor = fd;
	if(list == NULL)
	{
		temp->next = NULL;
		list = temp;
	}
	else
	{
		temp->next = list;
		list = temp;
	}
	return list;
}

struct trie* newTrieNode()
{
   struct trie *t = (struct trie*)malloc(sizeof(struct trie));
   t->substr = (char*)malloc(2);
   t->present_flag = 0;
   t->substr[0] = '\0';
   t->child = NULL;
   t->sibling = NULL;
   t -> list = NULL;
   return t;
}

struct trie* currentLevel(struct trie *t, char c)
{
   while(t != NULL)
   {
      if(t->substr[0] == c)
         return t;
      t = t->sibling;
   }
   return NULL;
}

void addWordInTrie(struct trie *t, char *str, struct file_descriptor *fd)
{
   int n = strlen(str);
   int i;

   for(i=0; i<n; i++)
   {
      char c = str[i];
      struct trie* parent = t;

      t = t->child;
      t = currentLevel(t,c);
      if(t == NULL)
      {
         t = newTrieNode();
         t->substr[0] = c;
         t->substr[1] = '\0';
         t->sibling = parent->child;
         parent->child = t;
      }
   }
   t->present_flag = 1;
   t->list = addtoList(t->list, fd);
   printf("word added\n");
}

void findInTrie(struct trie *t, char *str)
{
   int n = strlen(str);
   int i;
	printf("%s\n",str);
   for(i=0; i<n; i++)
   {
      char c = str[i];
      t = t->child;
      t = currentLevel(t,c);
      printf("%d\n", i);
   }
   exit(0);
   printlist(t->list);
//   return t->list;
}

void printlist(struct fdreturnList* list)
{
	struct fdreturnList* temp;
	temp = list;
	while(temp != NULL)
	{
		printf("%s\n", temp -> fdescriptor -> file_name);
		temp = temp->next;
	}
}

void mountTries(struct file_descriptor_list* FDList, struct trie* t)
{
   trieHead = NULL;
   trieHead =	newTrieNode();
   char name[20], ext[10] = "", word[20]="";
   int i, j, x;
   int k=0,count=0,size;
   struct block b;
   unsigned long int start;
   struct file_descriptor_list* temp;
   temp = FDList -> next;
   if(temp != NULL)
      while(temp != NULL)
      {
         size=(temp->fdescriptor).file_size;
         if(strcmp((temp->fdescriptor).file_type, "dir") != 0)
         {
            strcpy(name, (temp->fdescriptor).file_name);
          //  printf("%s\n", name);
            i = strlen(name)-1;
            while(name[i] != '.')
            {
               if(i == 0)
               break;
               i--;
            }
            i++;

            j=0;
            while(i<=strlen(name)-1)
            {
               ext[j] = name[i];
               j++;
               i++;
            }
            ext[j] == '\0';
          //  printf("%s\n", ext);
            if(strcmp(ext,"txt") == 0 || strcmp(ext,"c") == 0 || strcmp(ext,"h") == 0)
            {
               //yogesh: read the file here


               start = absoluteToblock((temp->fdescriptor).start_block_address);      
               while(fl.list[start]!=0xFFFF)
               {
   
                  fseek(fp,blockToabsolute(start),0);
                  fread(&b,sizeof(struct block),1,fp);
                  x=0;
                  for(k=0;k<(BLOCK_SIZE-4);k++)
                  {
                     while(b.data[k]!=' ')
                     {
                        word[x++] = b.data[k++];
                     }
                     k++;
                     word[x] = '\0';
                     printf("%s\n", word);
                     addWordInTrie(trieHead, word, &(temp->fdescriptor));
                     strcpy(word,"");
                     x=0;
                     count++; 
                  }  
                     start = fl.list[start];
               }
                  
                  fseek(fp,blockToabsolute(start),0);
                  fread(&b,sizeof(struct block),1,fp);
                  x=0;
                  for(k=0;(k<(BLOCK_SIZE-4) && (count<size));k++)
                  {  
                     while(b.data[k]!=' ')
                     {
                        word[x++] = b.data[k++];
                     }
                     k++;
                     word[x] = '\0';
                     printf("%s\n", word);
                     addWordInTrie(trieHead, word, &(temp->fdescriptor));
                     strcpy(word,"");
                     x=0;
                     count++;
                     printf("%d\n", k); 
                  }     
               
            }

         }
         temp = temp->next;
      }  
//printf("%s\n", "mountTries() done");
}

//int main()
//{
//	struct trie *trieHead;
//	int a;
//	trieHead =	newTrieNode();

//	return 0;
//}
